package com.ipay.wechat.service.impl;

import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ipay.utils.Constant;
import com.ipay.wechat.dao.ApplyDao;
import com.ipay.wechat.dao.BankInfoDao;
import com.ipay.wechat.pojo.BankInfo;
import com.ipay.wechat.pojo.MerchantInfo;
import com.ipay.wechat.service.ApplyService;

@Transactional
@Service("applyService") 
public class ApplyServiceImpl implements ApplyService {
	@Autowired
	private ApplyDao applyDao;
	@Autowired
	private BankInfoDao bankInfoDao;
	


	@Override
	public void addApplication(MerchantInfo merchantInfo) {
		// TODO Auto-generated method stub
		merchantInfo.setApplyTime(new Date());
		merchantInfo.setMerchantNo("0");
		applyDao.addApplication(merchantInfo);
	}


	@Override
	public MerchantInfo querryMerchantInfoByMerchantNo(String merchantNo) {
		// TODO Auto-generated method stub
		return applyDao.querryMerchantInfoByMerchantNo(merchantNo);
	}


	@Override
	public MerchantInfo querryMerchantInfoByMobile(String mobile) {
		return applyDao.querryMerchantInfoByMobile(mobile);
	}


	/**
	 * 根据当前登录的用户openid  查询对应的商户信息
	 * 微信登录表+收款员表+商户表
	 * @param openid
	 * @return
	 */
	@Override
	public MerchantInfo querryInfoByOpenid(String openid) {
		// TODO Auto-generated method stub
		return applyDao.querryInfoByOpenid(openid);
	}

	/**
	 * 商户入驻------第一步
	 * @param map
	 */
	@Override
	public void saveCompany(HashMap<String, String> map) {
		MerchantInfo info=new MerchantInfo();
		info.setShopname(map.get("s_name"));
		info.setShopAbbreviation(map.get("shopAbbreviation"));
		info.setShopAddress(map.get("s_addr"));
		info.setTelephone(map.get("telephone"));
		info.setUsername(map.get("username"));
		info.setMobilephone(map.get("mobile"));
		info.setEmail(map.get("email"));
		info.setOpenid(map.get("openid"));
		info.setMerchantNo(map.get("merchantNo"));
		info.setStatus(0);
		info.setTotalMoney(0);
		info.setIscreditCard(0);
		info.setPresentNub(1);
		applyDao.saveCompany(info);
	}
	/**
	 * 商户入驻------第一步 修改数据（上一步操作保存数据处理）
	 * @param map
	 */
	@Override
	public int saUpdateCompany(HashMap<String, String> map) {
		MerchantInfo info=new MerchantInfo();
		info.setShopname(map.get("s_name"));
		info.setShopAbbreviation(map.get("shopAbbreviation"));
		info.setShopAddress(map.get("s_addr"));
		info.setTelephone(map.get("telephone"));
		info.setUsername(map.get("username"));
		info.setMobilephone(map.get("mobile"));
		info.setEmail(map.get("email"));
		info.setOpenid(map.get("openid"));
		info.setMerchantNo(map.get("merchantNo"));
		info.setPresentNub(1);
		return applyDao.saUpdateCompany(info);
	}
    /**
     * 商户入驻------第二步
     * @param map
     */
    @Override
    public int saUpdateLegalPerson(HashMap<String, String> map) {
        MerchantInfo info=new MerchantInfo();
        info.setOpenid(map.get("openid"));
		info.setMerchantNo(map.get("merchantNo"));

		info.setLegalName(map.get("legalName"));
		info.setIdCard(map.get("idCard"));
		info.setPositiveImg(map.get("positive"));
		info.setOppositeImg(map.get("opposite"));
        info.setBusinessLicenseImg(map.get("businessLicense"));
        info.setBankLicenseImg(map.get("bankLicense"));
		info.setPresentNub(2);
		return applyDao.saUpdateLegalPerson(info);
    }
	/**
	 * 商户入驻------第三步
	 * @param map
	 */
	@Override
	public int saUpdateSettlingAccount(HashMap<String, String> map) {
		MerchantInfo info=new MerchantInfo();
		info.setOpenid(map.get("openid"));
		info.setMerchantNo(map.get("merchantNo"));
		info.setOuterImg(map.get("outer"));
		info.setSettlementBookImg(map.get("settlementBook"));
		info.setSettleUserOppositeImg(map.get("settleUserPositive"));
		info.setSettleUserPositiveImg(map.get("settleUserOpposite"));
		info.setSettleBankPositiveImg(map.get("settleBankPositive"));
		info.setSettleBankOppositeImg(map.get("settleBankOpposite"));
		info.setSealCardImg(map.get("sealCard"));
		info.setPresentNub(3);

		BankInfo bankInfo = new BankInfo();
		bankInfo.setBankCard(map.get("bankCard"));
		bankInfo.setBankName(map.get("bankName"));
		bankInfo.setBankUser(map.get("bankUser"));
		bankInfo.setMerchantNo(map.get("merchantNo"));
		bankInfo.setMobile(map.get("mobile"));
		bankInfo.setPayState(Constant.Status.ENABLE.getValue());
		bankInfo.setUseDesc("默认银行卡");
		bankInfoDao.save(bankInfo);

		return applyDao.saUpdateSettlingAccount(info);
	}



	@Override
	public void save(HashMap<String, String> map) {
		MerchantInfo info=new MerchantInfo();
		info.setShopname(map.get("s_name"));
		info.setShopAbbreviation(map.get("shopAbbreviation"));
		info.setShopAddress(map.get("s_addr"));
		info.setTelephone(map.get("telephone"));
		info.setUsername(map.get("username"));
		info.setMobilephone(map.get("mobile"));
		info.setEmail(map.get("email"));
		info.setOpenid(map.get("openid"));
		info.setInnerImg(map.get("inner"));
		info.setOuterImg(map.get("outer"));
		info.setOppositeImg(map.get("opposite"));
		info.setPositiveImg(map.get("positive"));
		info.setMerchantNo(map.get("merchantNo"));
		info.setStatus(0);
		info.setTotalMoney(0);
		info.setIscreditCard(0);
		
		info.setLegalName(map.get("legalName"));
		info.setIdCard(map.get("idCard"));
		info.setDoorHeadImg(map.get("doorHead"));
		info.setBusinessLicenseImg(map.get("businessLicense"));
		info.setBankLicenseImg(map.get("bankLicense"));
		info.setSettlementBookImg(map.get("settlementBook"));
		info.setSettleUserOppositeImg(map.get("settleUserPositive"));
		info.setSettleUserPositiveImg(map.get("settleUserOpposite"));
		info.setSettleBankPositiveImg(map.get("settleBankPositive"));
		info.setSettleBankOppositeImg(map.get("settleBankOpposite"));
		info.setSealCardImg(map.get("sealCard"));
		
		BankInfo bankInfo = new BankInfo();
		bankInfo.setBankCard(map.get("bankCard"));
		bankInfo.setBankName(map.get("bankName"));
		bankInfo.setBankUser(map.get("bankUser"));
		bankInfo.setMerchantNo(map.get("merchantNo"));
		bankInfo.setMobile(map.get("mobile"));
		bankInfo.setPayState(Constant.Status.ENABLE.getValue());
		bankInfo.setUseDesc("默认银行卡");
		bankInfoDao.save(bankInfo);
		
		applyDao.save(info);
	}


	@Override
	public void update(String merchantNo, String mobile) {
		// TODO Auto-generated method stub
		MerchantInfo info=new MerchantInfo();
		info.setMerchantNo(merchantNo);
		info.setMobilephone(mobile);
		applyDao.update(info);
	}


	@Override
	public MerchantInfo getInfoByOpenid(String openid) {
		return applyDao.getInfoByOpenid(openid);
	}


	@Override
	public void updateEmail(String merchantNo, String email) {
		MerchantInfo info=new MerchantInfo();
		info.setMerchantNo(merchantNo);
		info.setEmail(email);
		applyDao.update(info);
	}




}
